package com.ruoyi.gczl.sybg.cl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.sybg.cl.domain.SybgCl;
import com.ruoyi.gczl.sybg.cl.service.ISybgClService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 测量Controller
 *
 * @author huacai
 * @date 2024-08-21
 */
@RestController
@RequestMapping("/sybg/cl")
public class SybgClController extends BaseController {
    @Autowired
    private ISybgClService sybgClService;

    /**
     * 查询测量列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:cl:list')")
    @GetMapping("/list")
    public TableDataInfo list(SybgCl sybgCl) {
        startPage();
        List<SybgCl> list = sybgClService.selectSybgClList(sybgCl);
        return getDataTable(list);
    }

    /**
     * 导出测量列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:cl:export')")
    @Log(title = "测量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SybgCl sybgCl) {
        List<SybgCl> list = sybgClService.selectSybgClList(sybgCl);
        ExcelUtil<SybgCl> util = new ExcelUtil<SybgCl>(SybgCl.class);
        util.exportExcel(response, list, "测量数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SybgCl> util = new ExcelUtil<SybgCl>(SybgCl.class);
        util.importTemplateExcel(response, "测量数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "测量", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sybg:cl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SybgCl> util = new ExcelUtil<SybgCl>(SybgCl.class);
        InputStream inputStream = file.getInputStream();
        List<SybgCl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sybgClService.batchInsertSybgCl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取测量详细信息
     */
    @PreAuthorize("@ss.hasPermi('sybg:cl:query')")
    @GetMapping(value = "/{sybgClId}")
    public AjaxResult getInfo(@PathVariable("sybgClId") Integer sybgClId) {
        return success(sybgClService.selectSybgClBySybgClId(sybgClId));
    }

    /**
     * 新增测量
     */
    @PreAuthorize("@ss.hasPermi('sybg:cl:add')")
    @Log(title = "测量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SybgCl sybgCl) {
        sybgCl.setUserId(Math.toIntExact(getUserId()));
        sybgCl.setDeptId(Math.toIntExact(getDeptId()));
        sybgCl.setCreateBy(getUsername());
        return toAjax(sybgClService.insertSybgCl(sybgCl));
    }

    /**
     * 修改测量
     */
    @PreAuthorize("@ss.hasPermi('sybg:cl:edit')")
    @Log(title = "测量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SybgCl sybgCl) {
        return toAjax(sybgClService.updateSybgCl(sybgCl));
    }

    /**
     * 删除测量
     */
    @PreAuthorize("@ss.hasPermi('sybg:cl:remove')")
    @Log(title = "测量", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sybgClIds}")
    public AjaxResult remove(@PathVariable Integer[] sybgClIds) {
        return toAjax(sybgClService.deleteSybgClBySybgClIds(sybgClIds));
    }
}
